package com.example.demo.utils;

import com.example.demo.DynamicDataSource.DataSourceBean;
import com.example.demo.DynamicDataSource.DataSourceContext;
import com.example.demo.entity.DataSourceConfig;

/**
 * @author zhouzq1@chinaexpressair.com
 * @title
 * @date 2021/4/26 16:57
 **/
public class DynamicDataSourceUtil {

    /**
     * 根据数据库的配置动态设置数据源到配置中
     * @param dataSourceDO
     */
    public static void generateDynamicDataSource(DataSourceConfig dataSourceDO){
        DataSourceBean dataSourceBean = new DataSourceBean(new DataSourceBean.DataSourceBeanBuilder(dataSourceDO.getDataSourceName(),
                dataSourceDO.getDataSourceUrl(), dataSourceDO.getDataBaseUsername(), dataSourceDO.getDataBasePassword(),dataSourceDO.getReloadEveryTime()));

        DataSourceContext.setDataSource(dataSourceBean);
    }

    public static void clearDataSourceContext(){
        DataSourceContext.toDefault();
    }
}
